#!/bin/bash

cd `dirname $0`
root=`pwd`
today=`date +%F`

###
### Functions
###

# silently <command> <arg>*
function silently () {
    err=$("$@" </dev/null 2>&1)
    status=$?
    if [[ $status != 0 ]]; then
        echo "$err"
    fi
    return $status
}

# doing <action> <url>
function doing () {
    cols=$(tput cols 2>/dev/null || echo)
    if [[ -n $cols ]]; then
        echo "$1" "$2" | head -c $cols | xargs echo
    else
        echo "$1" "$2"
    fi
}

# downloading <url>
function downloading () {
    doing Downloading "$1"
}

# updating <url>
function updating () {
    doing Updating "$1"
}

# patching <url>
function patching () {
    doing Patching "$1"
}

# patching <url>
function compressing () {
    doing Compressing "$1"
}

# get_darcs <repo>
function get_darcs () {
    if [[ -d `basename "$1"` ]]; then
        pushd `basename "$1"` &>/dev/null &&
        updating "$1" &&
        silently darcs pull --all &&
        popd &>/dev/null
    else
        downloading "$1" &&
        silently darcs get "$1"
    fi
}

# get_svn <repo> [<dirname>]
function get_svn () {
    if [[ -z $2 ]]; then
        if [[ -d `basename "$1"` ]]; then
            pushd `basename "$1"` &>/dev/null &&
            updating "$1" &&
            silently svn up &&
            popd &>/dev/null
        else
            downloading "$1" &&
            silently svn co "$1"
        fi
    else
        if [[ -d $2 ]]; then
            pushd "$2" &>/dev/null &&
            updating "$1" &&
            silently svn up &&
            popd &>/dev/null
        else
            downloading "$1" &&
            silently svn co "$1" "$2"
        fi
    fi
}

# get_tgz <url>
function get_tgz () {
    url="$1" &&
    file=`basename \`echo "$url" | cut -d'?' -f1\`` &&
    name=`basename $file .tar.gz` &&
    downloading "$url" &&
    silently wget "$url" -O "$file" &&
    silently mkdir -p "$name" &&
    pushd "$name" &>/dev/null &&
    silently tar xfz "../$file" --strip-components 1 &&
    popd &>/dev/null &&
    rm -rf "$file"
}

# get_tgz_here <url>
function get_tgz_here () {
    url="$1" &&
    file=`basename \`echo "$url" | cut -d'?' -f1\`` &&
    name=`basename $file .tar.gz` &&
    downloading "$url" &&
    silently wget "$url" -O "$file" &&
    silently tar xfz "$file" &&
    silently rm -rf "$file"
}

# patch_file <dirname> <patch>
function patch_file () {
    pushd "$1" &>/dev/null &&
    patching "$2" &&
    silently patch -i "$root/patches/$2" -p1 &&
    popd &>/dev/null
}

# make_tgz <archive> <directory>
function make_tgz () {
    compressing "$1" &&
    silently tar cfz "$1" "$2"
}

# make_zip <archive> <directory>
function make_zip () {
    compressing "$1" &&
    silently zip -r "$1" "$2"
}

###
### Downloading and Updating
###

silently mkdir -p build/common &&
pushd build/common &>/dev/null &&

get_darcs http://common-lisp.net/project/cffi/darcs/cffi &&
get_darcs http://common-lisp.net/project/babel/darcs/babel &&
get_darcs http://common-lisp.net/project/alexandria/darcs/alexandria &&
get_darcs http://common-lisp.net/~loliveira/darcs/trivial-features &&
get_darcs http://common-lisp.net/~loliveira/darcs/trivial-garbage &&
get_darcs http://common-lisp.net/project/cl-store/darcs/cl-store &&
get_darcs http://common-lisp.net/project/closer/repos/closer-mop &&
get_darcs http://common-lisp.net/project/moptilities &&
get_darcs http://common-lisp.net/project/metatilities-base &&
get_darcs http://common-lisp.net/project/metatilities &&
get_darcs http://common-lisp.net/project/cl-containers &&
get_darcs http://common-lisp.net/project/cl-opengl/darcs/cl-opengl &&
get_darcs http://common-lisp.net/project/iterate/darcs/iterate &&

get_svn svn://common-lisp.net/project/usocket/svn/usocket/trunk usocket &&
get_svn http://lispbuilder.googlecode.com/svn/trunk/lispbuilder-sdl &&
get_svn http://lispbuilder.googlecode.com/svn/trunk/lispbuilder-sdl-gfx &&
get_svn http://lispbuilder.googlecode.com/svn/trunk/lispbuilder-sdl-image &&
get_svn http://lispbuilder.googlecode.com/svn/trunk/lispbuilder-sdl-mixer &&
get_svn http://lispbuilder.googlecode.com/svn/trunk/lispbuilder-sdl-ttf &&

get_tgz http://weitz.de/files/cl-fad.tar.gz &&
get_tgz "http://common-lisp.net/cgi-bin/viewcvs.cgi/cl-cli-parser.tar.gz?root=cl-cli-parser&view=tar" &&
get_tgz http://common-lisp.net/project/asdf-packaging/mt19937-latest.tar.gz &&
get_tgz http://ftp.linux.org.uk/pub/lisp/experimental/cclan/split-sequence.tar.gz &&
get_tgz http://www.method-combination.net/lisp/files/archive.tar.gz &&
get_tgz http://weitz.de/files/flexi-streams.tar.gz &&
get_tgz http://common-lisp.net/project/cl-plus-ssl/download/trivial-gray-streams.tar.gz &&

patch_file mt19937-latest mt19937.patch &&

silently cp -r $root/src/README . &&
silently cp -r $root/src/setup.lisp . &&

popd &>/dev/null &&

###
### Tarballs
###

silently rm -rf build/unix build/windows build/mac &&
silently mkdir -p build/unix &&
silently mkdir -p build/windows &&
silently mkdir -p build/mac &&
silently cp -r build/common build/unix/blackthorn-starter-pack &&
silently cp -r build/common build/windows/blackthorn-starter-pack &&
silently cp -r build/common build/mac/blackthorn-starter-pack &&

# Unix
pushd build/unix &>/dev/null &&
make_tgz "blackthorn-starter-pack-$today-unix.tar.gz" blackthorn-starter-pack &&
popd &>/dev/null &&

# Windows
pushd build/windows &>/dev/null &&
pushd blackthorn-starter-pack &>/dev/null &&
get_tgz_here http://lispbuilder.googlecode.com/files/win32-lispbuilder-sdl-binaries-1.2.14.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/win32-lispbuilder-sdl-gfx-binaries-2.0.13.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/win32-lispbuilder-sdl-image-binaries-1.2.10.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/win32-lispbuilder-sdl-mixer-binaries-1.2.11.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/win32-lispbuilder-sdl-ttf-binaries-2.0.9.tgz &&
popd &>/dev/null &&

make_zip "blackthorn-starter-pack-$today-windows.zip" blackthorn-starter-pack &&
popd &>/dev/null

# Mac
pushd build/mac &>/dev/null &&
pushd blackthorn-starter-pack &>/dev/null &&
get_tgz_here http://lispbuilder.googlecode.com/files/mac-lispbuilder-sdl-binaries-1.2.14.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/mac-lispbuilder-sdl-gfx-binaries-2.0.16.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/mac-lispbuilder-sdl-image-binaries-1.2.10.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/mac-lispbuilder-sdl-mixer-binaries-1.2.11.tgz &&
get_tgz_here http://lispbuilder.googlecode.com/files/mac-lispbuilder-sdl-ttf-binaries-2.0.10.tgz &&
popd &>/dev/null &&

make_tgz "blackthorn-starter-pack-$today-mac.tar.gz" blackthorn-starter-pack &&
popd &>/dev/null
